<template>
  <!-- 商品管理组件 -->
  <div class="shop">
    <!-- 面包屑路径导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>世界管理</el-breadcrumb-item>
      <el-breadcrumb-item>商店管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容区 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <div class="grid-content search-container">
            <el-input
              placeholder="请输入内容"
              v-model.trim="searchCont"
              clearable
              class="search-input"
              @clear="searchClear"
            >
            </el-input>
            <el-button
              type="info"
              plain
              icon="el-icon-search"
              @click="getShopList"
            ></el-button>
          </div>
        </el-col>
      </el-row>
      <!-- shop列表内容 -->
      <div class="shop-table">
        <el-table :data="shopList" border style="width: 100%">
          <!-- <el-table-column type="index" label="#" width="48"></el-table-column> -->
          <el-table-column
            prop="shopId"
            label="商店编号"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column label="商店NPC" align="center" width="100">
            <template slot-scope="props">
              <div v-if="props.row.shopData.length > 0">
                <el-image :src="path + props.row.shopData[0].npc_img">
                  <span slot="error">无NPC信息</span>
                </el-image>
                <div>{{ props.row.shopData[0].npc_name }}</div>
              </div>
              <div v-else>
                无NPC信息
              </div>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="shopItem_name"
            label="商品名称"
            align="center"
            width="120"
          >
          </el-table-column> -->
          <el-table-column label="商店类型" align="center" width="120">
            <template slot-scope="props">
              <div v-if="props.row.shopData.length > 0">
                <div v-if="props.row.shopData[0].shopItem_func">
                  {{ props.row.shopData[0].shopItem_func }}
                </div>
                <div v-else>
                  无类型
                </div>
              </div>
              <div v-else>
                无类型
              </div>
            </template>
          </el-table-column>
          <el-table-column label="商品列表" align="center">
            <template slot-scope="props">
              <div>
                <el-table :data="props.row.shopData" border stripe>
                  <el-table-column
                    prop="position"
                    label="序号"
                    width="48"
                    align="center"
                  >
                  </el-table-column>
                  <el-table-column label="图标" align="center" width="100">
                    <template slot-scope="props">
                      <div v-if="props.row.item_icon">
                        <el-image :src="path + props.row.item_icon">
                          <span slot="error">无</span>
                        </el-image>
                      </div>
                      <div v-else>
                        无
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="item_name"
                    label="商品名称"
                    width="100"
                  ></el-table-column>
                  <el-table-column label="商品分类" width="70">
                    <template slot-scope="props">
                      <el-tag v-if="props.row.category == 1" size="mini"
                        >特殊</el-tag
                      >
                      <el-tag
                        type="success"
                        v-if="props.row.category == 2"
                        size="mini"
                        >消耗品</el-tag
                      >
                      <el-tag
                        type="info"
                        v-if="props.row.category == 3"
                        size="mini"
                        >其它</el-tag
                      >
                      <el-tag
                        type="warning"
                        v-if="props.row.category == 4"
                        size="mini"
                        >设置</el-tag
                      >
                    </template>
                  </el-table-column>
                  <el-table-column prop="item_desc" label="商品描述">
                  </el-table-column>
                  <el-table-column prop="price" label="价格" width="100">
                  </el-table-column>
                  <el-table-column label="操作" width="120">
                    <template slot-scope="props">
                      <el-tooltip
                        class="item"
                        effect="dark"
                        content="修改商品"
                        placement="top"
                      >
                        <el-button
                          type="success"
                          size="mini"
                          icon="el-icon-setting"
                          @click="editShopItem(props.row.shopitemid)"
                        ></el-button>
                      </el-tooltip>
                      <el-tooltip
                        class="item"
                        effect="dark"
                        content="删除商品"
                        placement="top"
                      >
                        <el-button
                          size="mini"
                          type="warning"
                          icon="el-icon-delete"
                          @click="deleteShopItem(props.row.shopitemid)"
                        ></el-button>
                      </el-tooltip>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="props">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="editNpc(props.row.id)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteNpc(props.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageConfig.currentPage"
          :page-sizes="pageConfig.pageSizes"
          :page-size="pageConfig.pageSize"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageConfig.pageTotal"
          :hide-on-single-page="false"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 修改商品弹出层 -->
    <el-dialog
      title="修改商品"
      :visible.sync="dialogConfig.dialogEditShopItemVisible"
      class="edit-shopitem-dialog"
    >
      <el-form
        :model="editShopItemForm"
        :rules="editShopItemFormRules"
        ref="editShopItemForm"
        class="demo-ruleForm"
        :label-position="'right'"
        label-width="80px"
      >
        <el-form-item label="商品ID">
          <el-input
            v-model.trim="editShopItemForm.shopItemId"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="所属NPC">
          <el-input v-model.trim="editShopItemForm.shopNpc" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            v-model.trim="editShopItemForm.shopItemName"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="商品位置" prop="position">
          <el-input v-model.trim="editShopItemForm.position"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model.trim="editShopItemForm.price"></el-input>
        </el-form-item>

        <el-form-item style="text-align: right">
          <el-button @click="dialogConfig.dialogEditShopItemVisible = false"
            >取消</el-button
          >
          <el-button
            type="primary"
            @click="editShopItemSubmit('editShopItemForm')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索内容
      searchCont: '',
      // 商品列表数据
      shopList: [],
      // 资源路径
      path: '',
      // 分页器参数配置
      pageConfig: {
        pageTotal: 0, // 分页器总条数
        pageSize: 2, // 每页显示条数
        pageSizes: [1, 2, 3, 4, 5], // 可选择的每页条数
        currentPage: 1 // 分页器当前页码
      },
      // 标签页类型
      tabType: ['', 'success', 'warning', 'danger', 'info'],
      // 加载层参数配置
      loadingConfig: {
        target: 'body',
        fullscreen: true,
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.1)',
        text: '拼命加载中'
      },
      // 弹出层配置
      dialogConfig: {
        // 修改商品弹出层
        dialogEditShopItemVisible: false
      },
      // 编辑商品数据
      editShopItemForm: {
        itemId: null,
        shopNpc: '',
        shopItemName: '',
        price: 0,
        position: 0
      },
      // 编辑商品表单验证
      editShopItemFormRules: {
        position: [
          { validator: this.$store.state.checkNum, trigger: 'blur' },
          { required: true, message: '请输入商品的位置', trigger: 'blur' }
        ],
        price: [
          { validator: this.$store.state.checkNum, trigger: 'blur' },
          { required: true, message: '请输入商品的价格', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    getShopList () {
      // 获取商品列表
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      this.$axios
        .get('/shopList', {
          params: {
            search: this.searchCont.trim(),
            pagenum: this.pageConfig.currentPage,
            pagesize: this.pageConfig.pageSize
          }
        })
        .then(res => {
          if (res.status === 0) {
            // 处理数据
            console.log(res);
            // 设置分页
            this.pageConfig.pageTotal = res.total;
            this.pageConfig.currentPage = res.pagenum;
            // 设置数据
            this.shopList = res.data;
            this.path = res.path;
            // 判断当前页是否为空
            if (
              this.pageConfig.pageTotal ===
              (this.pageConfig.currentPage - 1) * this.pageConfig.pagesize
            ) {
              if (this.pageConfig.currentPage - 1 < 1) {
                this.handleCurrentChange(1);
              } else {
                this.handleCurrentChange(this.pageConfig.currentPage - 1);
              }
            }
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    searchClear () {
      this.getShopList();
    },
    calcTagType (index) {
      // 计算标签类型
      if (index < this.tabType.length) {
        return index;
      } else {
        return index % this.tabType.length;
      }
    },
    editShopItem (id) {
      // 根据ID查询商品信息
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      // 打开弹出层
      this.dialogConfig.dialogEditShopItemVisible = true;
      // 发起请求
      this.$axios
        .get('/shopItem/' + id)
        .then(res => {
          if (res.status === 0) {
            // 设置数据
            this.editShopItemForm.shopItemId = res.data.shopitemid;
            this.editShopItemForm.shopNpc = res.data.npc_name;
            this.editShopItemForm.shopItemName = res.data.item_name;
            this.editShopItemForm.shopItemDesc = res.data.item_desc;
            this.editShopItemForm.price = res.data.price;
            this.editShopItemForm.position = res.data.position;
          } else {
            // 提示失败信息
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    editShopItemSubmit (formName) {
      // 修改商品提交
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 打开加载层
          var loading = this.$loading.service(this.loadingConfig);
          this.$axios
            .put('/shopItem/' + this.editShopItemForm.shopItemId, {
              position: this.editShopItemForm.position,
              price: this.editShopItemForm.price
            })
            .then(res => {
              if (res.status === 0) {
                // 关闭弹出层
                this.dialogConfig.dialogEditShopItemVisible = false;
                // 提示成功信息
                this.$message.success(res.msg);
                // 重新刷新数据
                this.getShopList();
              } else {
                // 提示失败信息
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              // 请求失败
              this.$message.error(error);
            })
            .finally(() => {
              // 关闭加载层
              this.$nextTick(() => {
                loading.close();
              });
            });
        }
      });
    },
    deleteShopItem (id) {
      // 删除商品
      // 调用确认框
      this.$confirm('你确定要删除该商品吗？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      })
        .then(() => {
          // 打开加载层
          var loading = this.$loading.service(this.$store.state.loadingConfig);
          this.$axios
            .delete('/shopItem/' + id)
            .then(res => {
              if (res.status === 0) {
                // 提示成功信息
                this.$message.success(res.msg);
                // 重新刷新数据
                this.getShopList();
              } else {
                // 提示失败信息
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              // 请求失败
              this.$message.error(error);
            })
            .finally(() => {
              // 关闭加载层
              this.$nextTick(() => {
                loading.close();
              });
            });
        })
        .catch(() => {});
    },
    handleSizeChange (val) {
      // 切换每页条数重新获取数据
      this.pageConfig.pageSize = val;
      this.getShopList();
    },
    handleCurrentChange (val) {
      // 切换页码时重新获取数据
      this.pageConfig.currentPage = val;
      this.getShopList();
    }
  },
  mounted () {
    this.getShopList();
  }
};
</script>

<style lang="less" scoped>

.shopItem-table /deep/ .speak-tag {
  white-space: normal;
  max-width: 100%;
  min-height: 32px;
  height: auto;
}
</style>
